﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using ShopModel;

namespace ShopDAO
{
    public class VatRatesDAO
    {
        private string connectionString;

        public VatRatesDAO()
        {
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["csShop"].ConnectionString;
        }

        private SqlConnection Connection()
        {
            return new SqlConnection(connectionString);
        }

        /// <summary>
        /// Pobiera listę stawek vat.
        /// (Gets VAT rates list.)
        /// </summary>
        /// <returns>
        /// Zwraca listę obiektów VatRate zawierającą wszystkie stawki VAT w bazie.
        /// (Returns list of VatRate objects containing VAT rates in the database.)
        /// </returns>
        public List<VatRate> GetVatRates()
        {
            SqlConnection sqlC = Connection();
            List<VatRate> result = new List<VatRate>();

            try
            {
                SqlCommand com = new SqlCommand("GetVatRates", sqlC);
                com.CommandType = System.Data.CommandType.StoredProcedure;
                sqlC.Open();

                SqlDataReader sqlR = com.ExecuteReader();
                while (sqlR.Read())
                {
                    VatRate tmp = new VatRate((int)sqlR["IdVatRate"], (decimal)sqlR["Value"], (bool)sqlR["Default"]);
                    result.Add(tmp);
                }
                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            {
                sqlC.Close();
            }

            return result;
        }

        /// <summary>
        /// Pobiera informacje o wybranej stawce VAT.
        /// (Gets information about the selected  VAT rate.)
        /// </summary>
        /// <param name="VatRateID">
        /// ID stawki VAT.
        /// (ID of the VAT rate.)
        /// </param>
        /// <returns>
        /// Zwraca obiekt VatRate zawierający informacje o wybranej stawce VAT.
        /// (Returns VatRate object containing information about the selected VAT rate.)
        /// </returns>
        public VatRate GetVatRate(int VatRateID)
        {
            SqlConnection sqlC = Connection();
            VatRate result = null;

            try
            {
                SqlCommand com = new SqlCommand("GetVatRate", sqlC);
                com.Parameters.Add(new SqlParameter("@VatRateID", VatRateID));

                com.CommandType = System.Data.CommandType.StoredProcedure;
                sqlC.Open();

                SqlDataReader sqlR = com.ExecuteReader();
                if (sqlR.Read())
                    result = new VatRate((int)sqlR["IdVatRate"], (decimal)sqlR["Value"], (bool)sqlR["Default"]);

                sqlR.Close();
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            {
                sqlC.Close();
            }

            return result;
        }

        /// <summary>
        /// Zapisuje informacje o wybranej stawce VAT.
        /// (Saves information about the selected VAT rate.)
        /// </summary>
        /// <param name="vatRate">
        /// Obiekt VatRate zawierający informacje o stawce VAT.
        /// (VatRate object contains information about the VAT rate.)
        /// </param>
        /// <returns>
        /// Zwraca 'true' jeśli dodawanie zakonczyło się sukcesem.
        /// (Returns ‘true’ if adding succeeded.)
        /// </returns>
        public bool SaveVatRate(VatRate vatRate)
        {
            SqlConnection sqlC = Connection();
            bool result = false;

            try
            {
                SqlCommand com = new SqlCommand("SaveVatRate", sqlC);
                com.CommandType = System.Data.CommandType.StoredProcedure;

                if (vatRate.VatRateID.HasValue)
                    com.Parameters.Add(new SqlParameter("@VatRateID", vatRate.VatRateID.Value));

                com.Parameters.Add(new SqlParameter("@Value", vatRate.Value));
                com.Parameters.Add(new SqlParameter("@Default", vatRate.Default));

                sqlC.Open();

                SqlDataReader sqlR = com.ExecuteReader();

                if (sqlR.Read())
                    result = (bool)sqlR[0];
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            {
                sqlC.Close();
            }

            return result;
        }

        /// <summary>
        /// Usuwa wybraną stawkę podatku VAT.
        /// (Removes the selected VAT rate.)
        /// </summary>
        /// <param name="VatRateID">
        /// ID stawki VAT.
        /// (ID of the VAT rate.)
        /// </param>
        /// <returns>
        /// Zwraca 'true' jeśli usuwanie zakończyło się sukcesem.
        /// (Returns 'true' if the removal of data succeeded.)
        /// </returns>
        public bool DeleteVatRate(int VatRateID)
        {
            SqlConnection sqlC = Connection();
            bool result = false;
            
            try
            {
                SqlCommand com = new SqlCommand("DeleteVatRate", sqlC);
                com.CommandType = System.Data.CommandType.StoredProcedure;

                com.Parameters.Add(new SqlParameter("@VatRateID", VatRateID));
                sqlC.Open();

                SqlDataReader sqlR = com.ExecuteReader();

                if (sqlR.Read())
                    result = (bool)sqlR[0];
            }
            catch (Exception ex)
            {
                ErrorLogger.LogError(ex);
            }
            finally
            {
                sqlC.Close();
            }

            return result;
        }
    }
}
